package zelix.gui.clickguis.kendall;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.shader.Framebuffer;
import net.minecraft.client.shader.Shader;
import net.minecraft.client.shader.ShaderGroup;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Timer;
import zelix.utils.Mappings;
import zelix.utils.ReflectionHelper;
import zelix.utils.Wrapper;

import java.io.IOException;
import java.util.List;

public class BLur {

    private static ShaderGroup blurShader;


    private static Framebuffer buffer;
    private static int lastScale = 0;
    private static int lastScaleWidth = 0;
    private static int lastScaleHeight = 0;

    private static void reinitShader() {

        blurShader.createBindFramebuffers( Minecraft.getMinecraft().displayWidth,  Minecraft.getMinecraft().displayHeight);
        buffer = new Framebuffer( Minecraft.getMinecraft().displayWidth,  Minecraft.getMinecraft().displayHeight, true);
        buffer.setFramebufferColor(0.0f, 0.0f, 0.0f, 0.0f);
    }

    public static void draw(int x,int y,int width,int height,int blur,float radius){
        try {
            blurShader = new ShaderGroup( Minecraft.getMinecraft().renderEngine ,  Minecraft.getMinecraft().getResourceManager(),  Minecraft.getMinecraft().getFramebuffer(), new ResourceLocation("shaders/post/blur.json"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //Minecraft.getMinecraft().renderEngine.
        //sheng xia de yi qie jiu jiao gei Kendall lai shi xian la!
        //qianhe shishikeke zhun bei zhe wei Kendall de code zhao yi ge xin jia = skid
        ScaledResolution scale = new ScaledResolution(Minecraft.getMinecraft());
        int factor = scale.getScaleFactor();
        int factor2 = scale.getScaledWidth();
        int factor3 = scale.getScaledHeight();
        if (lastScale != factor || lastScaleWidth != factor2 || lastScaleHeight != factor3) {
            reinitShader();
        }
        lastScale = factor;
        lastScaleWidth = factor2;
        lastScaleHeight = factor3;
        List<Shader> listShaders = ReflectionHelper.getPrivateValue(ShaderGroup.class,blurShader,"listShaders");
        listShaders.get(0).getShaderManager().getShaderUniform("BlurXY").set(x, factor3 - y - height);
        listShaders.get(1).getShaderManager().getShaderUniform("BlurXY").set(x, factor3 - y - height);
        listShaders.get(0).getShaderManager().getShaderUniform("BlurCoord").set(width,height);
        listShaders.get(1).getShaderManager().getShaderUniform("BlurCoord").set(width,height);
        listShaders.get(0).getShaderManager().getShaderUniform("Radius").set(radius);
        listShaders.get(1).getShaderManager().getShaderUniform("Radius").set(radius);
        Timer timer = ReflectionHelper.getPrivateValue(Minecraft.class, Wrapper.INSTANCE.mc(), new String[] { Mappings.timer });
        blurShader.render(timer.renderPartialTicks);
        Minecraft.getMinecraft().getFramebuffer().bindFramebuffer(true);
    }


}
